﻿using System;
using System.Collections.Generic;
using System.Data;
using SubSonic.Schema;

namespace MvcForum.Net.Business
{
    public class TopicsRepository
    {

        public static List<TopicEntity> List(int forumId, bool isModerator, int pageIndex, int pageSize, out int totalRows)
        {


            var _sp = new MainDB().TopicsList(forumId, isModerator, pageIndex, pageSize);

            var _list = _sp.ExecuteTypedList<TopicEntity>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
            return _list;


        }


        public static List<TopicEntity> QuickSearch(int forumId, string keywords, short resultType, int maxResults)
        {


            var _sp = new MainDB().QuickSearch(forumId, keywords, resultType, maxResults);

            return _sp.ExecuteTypedList<TopicEntity>();

        }


        public static List<TopicEntity> AdvandedSearch(string sqlScripts)
        {


            return new MainDB().AdvancedSearch(sqlScripts).ExecuteTypedList<TopicEntity>();


        }



        public static List<TopicEntity> RssFetch(int forumId, int fecthItems)
        {


            return new MainDB().TopicsRssFetch(forumId, fecthItems).ExecuteTypedList<TopicEntity>();


        }


        //public static void Thread(int topicId, int pageIndex, int pageSize, out  int totalRows,out List<ThreadEntity> Posts,out List<ThankEntity> Thanks)
        //{



        //    var _sp = new MainDB().TopicsPostsList(topicId, pageIndex, pageSize);

        //    IDbCommand cmd;



        //    using (var rdr = _sp.ExecuteReader())
        //    {
        //        Posts=rdr.

        //    }

        //    var _list = _sp.ExecuteTypedList<ThreadEntity>();

        //    totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
        //    return _list;
        //}


        public static List<ThreadEntity> Thread(int topicId, int pageIndex, int pageSize,bool isMod, out  int totalRows)
        {



            var _sp = new MainDB().TopicsPostsList(topicId, pageIndex, pageSize,isMod);

            var _list = _sp.ExecuteTypedList<ThreadEntity>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);
            return _list;
        }

        public static List<ThankEntity> Thanks(int topicId, int pageIndex, int pageSize)
        {



            var _sp = new MainDB().TopicThankList(topicId, pageIndex, pageSize);

            return _sp.ExecuteTypedList<ThankEntity>();



        }


        public static void ThankSave(int topicId, int userId, string username, int action)
        {



            new MainDB().ThanksSave(topicId, userId, username, action).Execute();

        }





        public static TopicEntity Detail(int topicId)
        {
            var _sp = new MainDB().TopicDetail(topicId);

            var _list = _sp.ExecuteTypedList<TopicEntity>();

            return _list.Count > 0 ? _list[0] : null;
        }



        public static TopicEntity FindPost(int topicId, out int totalRows)
        {
            var _sp = new MainDB().FindPost(topicId);

            var _list = _sp.ExecuteTypedList<TopicEntity>();

            totalRows = Convert.ToInt32(_sp.OutputParameters.GetParameter("TotalRows").ParameterValue);

            return _list.Count > 0 ? _list[0] : null;
        }



        public static void Add(int? topicId, string topicName, int forumId, int? pollId, string icon, string message, int? replyTo, int priority, DateTime? priorityDate,
            bool isApproved, string editedBy, string remoteUrl, string userName, int userId, string ip, out int Id)
        {

            var _db = new MainDB();

            var sp = new StoredProcedure("MF_TopicsSave", _db.Provider);
            if (topicId.HasValue)
                sp.Command.AddParameter("TopicId", topicId, DbType.Int32, ParameterDirection.Input);
            sp.Command.AddParameter("TopicName", topicName, DbType.String, ParameterDirection.Input);
            sp.Command.AddParameter("ForumId", forumId, DbType.Int32, ParameterDirection.Input);
            if (pollId.HasValue)
                sp.Command.AddParameter("PollId", pollId, DbType.Int32, ParameterDirection.Input);
            sp.Command.AddParameter("Icon", icon, DbType.AnsiString, ParameterDirection.Input);
            sp.Command.AddParameter("Message", message, DbType.String, ParameterDirection.Input);
            if (replyTo.HasValue)
                sp.Command.AddParameter("ReplyTo", replyTo, DbType.Int32, ParameterDirection.Input);
            sp.Command.AddParameter("Priority", priority, DbType.Int32, ParameterDirection.Input);
            if (priorityDate.HasValue)
                sp.Command.AddParameter("PriorityDate", priorityDate, DbType.DateTime, ParameterDirection.Input);
            sp.Command.AddParameter("IsApproved", isApproved, DbType.Boolean, ParameterDirection.Input);
            if (!string.IsNullOrEmpty(editedBy))
                sp.Command.AddParameter("EditedBy", editedBy, DbType.String, ParameterDirection.Input);
            if (!string.IsNullOrEmpty(remoteUrl))
                sp.Command.AddParameter("RemoteUrl", remoteUrl, DbType.String, ParameterDirection.Input);
            sp.Command.AddParameter("UserName", userName, DbType.String, ParameterDirection.Input);
            sp.Command.AddParameter("UserId", userId, DbType.Int32, ParameterDirection.Input);
            sp.Command.AddParameter("IP", ip, DbType.AnsiString, ParameterDirection.Input);

            sp.Command.AddOutputParameter("RETURN_VALUE", DbType.Int32);



            sp.Execute();

            Id = Convert.ToInt32(sp.OutputParameters.GetParameter("RETURN_VALUE").ParameterValue);

        }
    }
}
